package com.xuecheng.content.controller;

import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.system.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class CourseBaseController implements CourseBaseApi {

    @Autowired
    private CourseBaseService  courseBaseService;

    /**
     *
     * 默认使用queryString来接收参数 PageRequestParams 是不用添加注解的 但是里面的属性必须和传来的参数的key一致
        如果接收RequestBody参数 需要通过RequestBody注解
     */
    @PostMapping("course/list")
    public PageVO queryCourseBaseList(PageRequestParams params,
                                      @RequestBody QueryCourseModel model) {
        //1.获得令牌并解析 获取companyId
        Long companyId = SecurityUtil.getCompanyId();
        //2. 调用service层获取结果
        PageVO pageVO = this.courseBaseService.queryCourseBaseList(params, model, companyId);
        //3.返回结果

        return pageVO;
    }

    /**
     *
     * @param vo
     * @return
     */
    @PostMapping("course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVO vo) {
        CourseBaseDTO dto = CourseBaseConvert.INSTANCE.vo2dto(vo);
        //获取companyId的工具类 利用线程
        Long companyId = SecurityUtil.getCompanyId();
        dto.setCompanyId(companyId);
        return this.courseBaseService.createCourseBase(dto);

    }

    @GetMapping("/course/{courseBaseId}")
    public CourseBaseDTO getCourseBase(@PathVariable Long courseBaseId) {
        Long companyId = SecurityUtil.getCompanyId();
        return this.courseBaseService.getCourseBase(courseBaseId,companyId);

    }

    @PutMapping("/course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        CourseBaseDTO dto = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVO);
        //获取companyId的工具类 利用线程
        Long companyId = SecurityUtil.getCompanyId();
        dto.setCompanyId(companyId);
        return this.courseBaseService.modifyCourse(dto);
    }

    @DeleteMapping("course/{courseBaseId}")
    public void removeCoursebase(@PathVariable Long courseBaseId) {
        Long companyId = SecurityUtil.getCompanyId();
        this.courseBaseService.removeCoursebase(courseBaseId,companyId);
    }
}
